<?php
// +----------------------------------------------------------------------
// | title: 标题注释
// +----------------------------------------------------------------------
// | Author: TZ.Leong
// +----------------------------------------------------------------------
// | Date: 2019/7/3 11:54
// +----------------------------------------------------------------------

namespace app\single\model;


use Exception;
use  mycommon\src\MongoDBHelper;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\db\Where;
use think\exception\DbException;
use think\facade\Cache;
use think\Model;
use mycommon\src\TCPHelper;
use think\facade\Log;

class RoleModel extends Model
{


    public function initialize()
    {
        $this->table = config("database.game") . ".role";

    }

    /**
     * @return array
     * @throws Exception
     */
//    public function queryRole(): array
//    {
//        $server = Cache::store("file")->get("server_info");
//        if (empty($server)) {
//            throw new Exception('Exception message');
//        }
////        $mongo = new MongoDBHelper();
////        return $mongo->find($this->colsName);
//    }

    /**
     * 获取当前在线情况，分平台
     * @param array $map
     * @return array
     */
    public function getCurrentOnline(array $map): array
    {
        $res = self::where($map)->group('pf')->column("pf as platform,COUNT(*) AS num,COUNT(DISTINCT last_login_ip) AS ip_num");
        echo "debug somethings last sql :" . $this->getLastSql().PHP_EOL;
        return $res;
    }

    /**
     * 系统邮件按照条件查询获取玩家
     * @param $where
     * @return mixed
     */
    public function getMailPlayerInfo($where)
    {
        //查询
         //查询
         try {
            return $this->alias("a")
//                ->leftJoin([ "role_pay_total" => "b"], "b.role_id = a.id")
//                ->leftJoin([config("database.game") . ".role_vip" => "c"], "c.role_id = a.id")
                ->where(new Where($where))->field("a.id ,a.name")
                ->order("a.lev DESC")->select();
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            echo "error".$e->getMessage();
            return [];
        }
    }

    /**
     * 元宝库存
     * @return array|\PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function statGoldStore()
    {
        //        var_dump($this->getLastSql());
        return $this->alias("r")
            ->join([config("database.log") . ".log_assets" => "re"], "re.role_id = r.id and item_id =2 ")
            ->field("r.platform,SUM(re.item_id) AS gold")
            ->group("r.platform")
            ->select();
    }

    public function statXianyuStore()
    {
        $data = $this->alias("r")
            ->join([config("database.log") . ".log_assets" => "re"], "re.role_id = r.id and type =88 ")
            ->field("r.pf as platform,max(re.new_value) AS gold")
            ->group("r.platform")
            ->select();
//        var_dump($this->getLastSql());
        return $data;
    }
}